package com.cricked.parser;

/**
 * @author aizaz.haider
 */
import java.util.List;

import org.w3c.dom.Text;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity implements IPublishResultListener {

	private TableLayout tlRoot;
	private ProgressBar progbar;
	private TextView tvErrorMsg;

	// ------------------
	private TextView tvLeftHeading1;
	private TextView tvLeftHeading2;
	private TextView tvLeftHeading3;

	private TextView tvRightHeading1;
	private TextView tvRightHeading2;

	private TableRow trBatsmenValue1;
	private TableRow trBatsmenValue2;

	private TableRow trBowlerValue1;
	private TableRow trBowlerValue2;

	private TextView tvBottomValue1;
	private TextView tvBottomValue2;
	private TextView tvBottomValue3;

	// ------------------

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_main);
		setContentView(R.layout.score_table);
		this.implementDefaults();

		PublishResultHelper.getInstance().addListener(this);
		_Parser parser = new _Parser();
		parser.parseCricketFeed("http://www.espncricinfo.com/tri-nation-west-indies-2013/engine/match/597927.html");

	}

	@Override
	public void publishResult(List<CricketFeedInfo> pList) {
		if (pList.get(0).tableRow1Heading == null) {
			tvErrorMsg.setVisibility(View.VISIBLE);
			progbar.setVisibility(View.GONE);
			return;
		}
		CricketFeedInfo crickedFeedInfo = pList.get(0);

		// this.setValue(crickedFeedInfo.leftDiv);
		if (crickedFeedInfo.leftDiv != null) {
			try {
				tvLeftHeading1.setText(crickedFeedInfo.leftDiv[0]);
				tvLeftHeading2.setText(crickedFeedInfo.leftDiv[1]);
				tvLeftHeading3.setText(crickedFeedInfo.leftDiv[2]);
			} catch (NullPointerException npe) {
//				Log.w("CricketFeedParser", npe.getMessage());
			}

		}

		// this.setValue(crickedFeedInfo.rightDiv);
		if (crickedFeedInfo.rightDiv != null) {
			try {
				tvRightHeading1.setText(crickedFeedInfo.rightDiv[0]);
				tvRightHeading2.setText(crickedFeedInfo.rightDiv[1]);
			} catch (NullPointerException npe) {
//				Log.w("CricketFeedParser", npe.getMessage());
			}

		}

		// this.setValue(crickedFeedInfo.tableRow1Heading);
		// this.setValue(crickedFeedInfo.tableRow1Values1);
		{
			if (crickedFeedInfo.tableRow1Values1 != null) {
				for (int i = 0; i < crickedFeedInfo.tableRow1Values1.length; i++) {
					try {
						((TextView) trBatsmenValue1.getChildAt(i)).setText(crickedFeedInfo.tableRow1Values1[i]);
					} catch (NullPointerException npe) {
						//Log.w("CricketFeedParser", npe.getCause());
					}

				}
			}

		}

		// this.setValue(crickedFeedInfo.tableRow1Values2);
		{
			if (crickedFeedInfo.tableRow1Values2 != null) {
				for (int i = 0; i < crickedFeedInfo.tableRow1Values2.length; i++) {
					try {
						((TextView) trBatsmenValue2.getChildAt(i)).setText(crickedFeedInfo.tableRow1Values2[i]);
					} catch (NullPointerException npe) {
//						Log.w("CricketFeedParser", npe.getCause().toString());
					}

				}
			}

		}

		// this.setValue(crickedFeedInfo.tableRow2Heading);
		// this.setValue(crickedFeedInfo.tableRow2Values1);
		{
			if (crickedFeedInfo.tableRow2Values1 != null) {
				for (int i = 0; i < crickedFeedInfo.tableRow2Values1.length; i++) {
					try {
						((TextView) trBowlerValue1.getChildAt(i)).setText(crickedFeedInfo.tableRow2Values1[i]);
					} catch (NullPointerException npe) {
//						Log.w("CricketFeedParser", npe.getMessage());
					}

				}
			}
		}
		// this.setValue(crickedFeedInfo.tableRow2Values2);
		{
			if (crickedFeedInfo.tableRow2Values2 != null) {
				for (int i = 0; i < crickedFeedInfo.tableRow2Values2.length; i++) {
					try {
						((TextView) trBowlerValue2.getChildAt(i)).setText(crickedFeedInfo.tableRow2Values2[i]);
					} catch (NullPointerException npe) {
//						Log.w("CricketFeedParser", npe.getMessage());
					}

				}
			}

		}
		// this.setValue(crickedFeedInfo.otherInfo);
		if (crickedFeedInfo.otherInfo != null) {
			try {
				tvBottomValue1.setText(crickedFeedInfo.otherInfo[0]);
				tvBottomValue2.setText(crickedFeedInfo.otherInfo[1]);
				tvBottomValue3.setText(crickedFeedInfo.otherInfo[2]);
			} catch (NullPointerException npe) {
//				Log.w("CricketFeedParser", npe.getMessage());
			}

		}

		progbar.setVisibility(View.GONE);
		tlRoot.setVisibility(View.VISIBLE);
	}

	// private void setValue(String[] arr) {
	//
	// for (int i = 0; i < arr.length; i++) {
	// TextView tv = new TextView(this);
	// tv.setText(arr[i]);
	// llContainer.addView(tv);
	// }
	// }

	private void implementDefaults() {
		tvErrorMsg = (TextView) findViewById(R.id.tv_error_msg);
		tlRoot = (TableLayout) findViewById(R.id.tl_root);
		progbar = (ProgressBar) findViewById(R.id.pb_progress);

		tvLeftHeading1 = (TextView) findViewById(R.id.tv_left_heading_1);
		tvLeftHeading2 = (TextView) findViewById(R.id.tv_left_heading_2);
		tvLeftHeading3 = (TextView) findViewById(R.id.tv_left_heading_3);

		tvRightHeading1 = (TextView) findViewById(R.id.tv_right_heading_1);
		tvRightHeading2 = (TextView) findViewById(R.id.tv_right_heading_2);

		trBatsmenValue1 = (TableRow) findViewById(R.id.tr_batsmen_value_1);
		trBatsmenValue2 = (TableRow) findViewById(R.id.tr_batsmen_value_2);

		trBowlerValue1 = (TableRow) findViewById(R.id.tr_bowler_value_1);
		trBowlerValue2 = (TableRow) findViewById(R.id.tr_bowler_value_2);

		tvBottomValue1 = (TextView) findViewById(R.id.tv_bottom_heading_1);
		tvBottomValue2 = (TextView) findViewById(R.id.tv_bottom_heading_2);
		tvBottomValue3 = (TextView) findViewById(R.id.tv_bottom_heading_3);
	}

}
